﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.Order.OrderGoodsChecklist;

namespace BCData.TMS.Order.OrderGoodsChecklist
{
    public class OrderGoodsChecklistData : IOrderGoodsChecklistData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.OrderGoodsChecklistId,
		    A.TransportOrderId,
		    A.TransportOrderNo,
		    A.OrderId,
		    A.OrderNo,
		    A.MallOrderNo,
		    A.StorageOrderNo,
		    A.DriverRemark,
		    A.Shipping,
		    A.ShippingPhone,
		    A.ShippingAddress,
		    A.Consignee,
		    A.ConsigneePhone,
		    A.ConsigneeAddress,
		    A.ProvinceCode,
		    A.ProvinceName,
		    A.CityCode,
		    A.CityName,
		    A.CountyCode,
		    A.CountyName,
		    A.Longitude,
		    A.Latitude,
		    A.EndProvinceCode,
		    A.EndProvinceName,
		    A.EndCityCode,
		    A.EndCityName,
		    A.EndCountyCode,
		    A.EndCountyName,
		    A.EndLongitude,
		    A.EndLatitude,
		    A.GoodsSKU,
		    A.GoodsPhoto,
		    A.GoodsName,
		    A.GoodsWeight,
		    A.GoodsVolume,
		    A.GoodsLength,
		    A.GoodsWidth,
		    A.GoodsHeight,
		    A.GoodsCount,
		    A.GoodsPrice,
		    A.GoodsUnit,
		    A.MallOrderAmount,
		    A.InsuredClaimPayAmount,
		    A.IsCod,
		    A.CodCost,
		    A.CodCharge,
		    A.CodGoodsValueCertificates,
		    A.CodCostAmount,
		    A.CodChargeAmount,
		    A.InsuredPriceCertificates,
		    A.InsuredPrice,
		    A.InsuredPriceCharge,
		    A.IsInsuredPriceHigh,
		    A.InsuredClaimWeight,
		    A.InsuredClaimCount,
		    A.InsuredClaimPay,
		    A.InsuredPriceAmount,
		    A.InsuredPriceChargeAmount,
		    A.TimeoutClaimPayAmount,
		    A.IsInsuredPrice,
		    A.OrderAmount,
		    A.FreightCostAmount,
		    A.TotalDistanceLength,
		    A.NotifyDriveTime,
		    A.DriveOrderPromising,
		    A.VehicleStartOutTime,
		    A.CloseOrderTime,
		    A.CloseOrderContent,
		    A.ShippingPayTime,
		    A.ConsigneePayTime
        FROM
            OrderGoodsChecklist A";



        #region 构造函数
        public OrderGoodsChecklistData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新订单货物清单纪录
        /// </summary>
        /// <param name="orderGoodsChecklistEntity"></param>
        /// <returns></returns>
        public async Task<OrderGoodsChecklistEntity> InsertAsync(OrderGoodsChecklistEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改订单货物清单记录
        /// </summary>
        /// <param name="orderGoodsChecklistEntity"></param>
        /// <returns></returns>
        public async Task<OrderGoodsChecklistEntity> UpdateAsync(OrderGoodsChecklistEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除订单货物清单记录
        /// </summary>
        /// <param name="orderGoodsChecklistId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long orderGoodsChecklistId)
        {
            string sql = "DELETE FROM ordergoodschecklist WHERE OrderGoodsChecklistId = ?orderGoodsChecklistId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderGoodsChecklistId", orderGoodsChecklistId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取订单货物清单记录
        /// </summary>
        /// <param name="orderGoodsChecklistId"></param>
        /// <returns></returns>
        public async Task<OrderGoodsChecklistEntity> GetEntityAsync(long orderGoodsChecklistId)
        {
            string sql = QUERY + " WHERE  A.OrderGoodsChecklistId = ?orderGoodsChecklistId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderGoodsChecklistId", orderGoodsChecklistId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<OrderGoodsChecklistEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询订单货物清单记录
        public async Task<(IEnumerable<OrderGoodsChecklistEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  ordergoodschecklist  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<OrderGoodsChecklistEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口



